package com.xiaoxin.experience.service.sdk.rtsp;

import com.xiaoxin.experience.util.DateUtil;

import java.util.Date;
import java.util.TimeZone;


/**
 * @author xiaoxin
 * Simple usage example:
 *   String liveRtspUrl = new HikRtspUrlBuilder("ip","password").build();
 *   String recordRtspUrl = new HikRtspUrlBuilder("ip","password").record("startTime","endTime").build();
 */
public class HikRtspUrlBuilder extends RtspUrlBuilder
{
    private static final String FORMAT_UTC = "yyyyMMdd'T'HHmmss'Z'";
    /**
     * 是否为ipc设备,默认为true
     */
    private boolean isIpc = true;

    /**
     * rtsp拼接规则默认使用2012年的拼接规则
     */
    private SplicingRules splicingRules = SplicingRules.RULES_2012;

    private String datePattern = DateUtil.YMDHMS_STD;

    private String serverTimezone = "GMT+8";

    private String puTimezone = "GMT";

    public HikRtspUrlBuilder(String ip, String password)
    {
        super(ip,password);
    }

    public HikRtspUrlBuilder isIpc()
    {
        this.isIpc = true;
        return this;
    }

    public HikRtspUrlBuilder isNvr()
    {
        this.isIpc = false;
        return this;
    }

    public HikRtspUrlBuilder splicingRules(SplicingRules splicingRules)
    {
        this.splicingRules = splicingRules;
        return this;
    }

    public HikRtspUrlBuilder datePattern(String datePattern)
    {
        this.datePattern = datePattern;
        return this;
    }

    public HikRtspUrlBuilder timezone(String severTimezone, String puTimezone)
    {
        this.serverTimezone = severTimezone;
        this.puTimezone = puTimezone;
        return this;
    }

    protected String buildLiveRtspUrl()
    {
        String rtspUrl = null;
        switch (splicingRules)
        {
            case RULES_2012:
                rtspUrl = commonPrefix() +
                        "/Streaming/Channels/" + channelNo + 0 + streamType.value() + "?transportmode=unicast";
                break;
            case RULES_2012_PRE:
                rtspUrl = commonPrefix() + "/h264/ch";
                if (isIpc)
                {
                    rtspUrl += channelNo;
                }
                else
                {
                    rtspUrl += (channelNo + 32);
                }
                rtspUrl += "/" + streamType.flag() + "/av_stream";
                break;
            default:
                break;
        }
        return rtspUrl;
    }

    protected String buildRecordUrl()
    {
        return  commonPrefix() +
                "/Streaming/tracks/" + channelNo + 0 + streamType.value() +
                "?starttime=" + adapt(startTime) + "&endtime=" + adapt(endTime);
    }

    public String adapt(String time)
    {
        Date date = DateUtil.timeStr2Date(time, datePattern);
        return DateUtil.transDateBetweenTimeZone(date,
                FORMAT_UTC,
                TimeZone.getTimeZone(serverTimezone),
                TimeZone.getTimeZone(puTimezone));
    }

    /**
     * 拼接规则
     */
    public enum SplicingRules
    {
        /**
         * 2012年之前的hik设备rtsp拼接规则
         */
        RULES_2012_PRE,

        /**
         * 2012年规定的hik设备rtsp拼接规则
         */
        RULES_2012
    }

}
